package com.example.administrator.myapplication.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.base.BaseActivity;
import com.example.administrator.myapplication.mode.InformationDataManager;
import com.example.administrator.myapplication.utils.ToastUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import me.nereo.multi_image_selector.MultiImageSelector;
import me.nereo.multi_image_selector.MultiImageSelectorActivity;

import static com.example.administrator.myapplication.view.HomeActivity.REQUEST_CODE;


/**
 * 说明：添加资讯
 * 作者： 阳2012; 创建于： 2017/5/15 23:34
 */
public class AddInformationActivity extends BaseActivity {
    @BindView(R.id.phone)
    TextInputLayout phone;
    @BindView(R.id.password)
    TextInputLayout password;
    @BindView(R.id.commitLogin)
    TextView commitLogin;

    @BindView(R.id.url)
    TextInputLayout url;

    @BindView(R.id.addPicture)
    ImageView addPicture;
    ArrayList<String> picturePath = new ArrayList<>();

    @Override
    public int getContentView() {
        return R.layout.activity_add_information;
    }


    public static void startUI(Context context) {
        Intent intent = new Intent(context, AddInformationActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    public void parseIntent(Intent intent) {

    }

    /*
     * 初始化view
     */
    public void initView() {
        toolbar.setTitle("添加资讯");
        toolbar.setNavigationIcon(R.drawable.material_back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }


    @OnClick({R.id.addPicture, R.id.commitLogin})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.addPicture:
                MultiImageSelector.create()
                        .showCamera(true)
                        .single()
                        .origin(picturePath)
                        .start(this, REQUEST_CODE);
                break;
            case R.id.commitLogin:
                if (phone.getEditText().getText().toString().length() == 0) {
                    ToastUtils.getToastLong("标题不能为空");
                    return;
                }
                if (password.getEditText().getText().toString().length() == 0) {
                    ToastUtils.getToastLong("内容不能为空");
                    return;
                }
                if (url.getEditText().getText().toString().length() == 0) {
                    ToastUtils.getToastLong("网址不能为空");
                    return;
                }

                if (picturePath.size() == 0) {
                    ToastUtils.getToastLong("照片不能为空");
                    return;
                }

                /**
                 * @param title
                 * @param content
                 * @param url
                 * @param imgPath
                 * @return
                 */
                InformationDataManager.getInstands().save(
                        this,
                        phone.getEditText().getText().toString(),
                        password.getEditText().getText().toString(),
                        url.getEditText().getText().toString(),
                        picturePath.get(0)

                );

                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        //过程图片路径
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            picturePath.clear();
            picturePath.addAll(data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT));

            Glide
                    .with(this)
                    .load(picturePath.get(0))
                    .into(addPicture);

        }
    }
}
